package com.bomberdroid.protocols;

import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;

/** This class manages everything involving connexions between bluetooth devices */
public class Connexion {
	
	/** Arbitrary UUID for connexion purposes */
	public static UUID uuid = UUID.fromString("88edec38-d13c-46dc-87f2-9e04dada0035");
	
	/** Arbitrary int used for the callback of onActivityResult() when activating bluetooth */
	public static final int REQUEST_ENABLE_BT = 1653745;
	
	/** Arbitrary int used for the callback of onActivityResult() when activating discoverability */
	public static final int REQUEST_DISCOVERABLE_BT = 78945;
	
	/**	The context using this class */
	private Activity contextActivity;
	
	/** Bluetooth Adapter use for everything involving bluetooth*/
	private BluetoothAdapter bluetoothAdapter;

	/** Constructor 
	 * @param c The Context using this class (generaly this);
	 */
	public Connexion(Activity a) {
		this.contextActivity = a;
		this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		this.bluetoothAdapter.cancelDiscovery();
	}
	
	/** Checks if bluetooth is enabled, and prompt the user
	 * @return the current state of the bluetooth (before the user is prompted)
	 */
	public boolean checkBluetooth(){
		if(!this.bluetoothAdapter.isEnabled()){
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    this.contextActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		return this.bluetoothAdapter.isEnabled();
	}
	
	/** get the identity of the bluetooth adapter */
	public String getIdentity(){
		return (this.bluetoothAdapter.getName()+"\n"+this.bluetoothAdapter.getAddress()).replace(";", "").replace("&", "");
	}

	public BluetoothAdapter getBluetoothAdapter() {
		return bluetoothAdapter;
	}

	public void setBluetoothAdapter(BluetoothAdapter bluetoothAdapter) {
		this.bluetoothAdapter = bluetoothAdapter;
	}
}
